const query = require("../db/mysql")
const { success, error } = require("../module/module")

class Home {
    getHome = async (ctx) => {
        let newSql = 'select count(id) as total from newHouse '
        let oldSql = 'select count(id) as total from twoHoust '
        let secondarySql = 'select count(id) as total from House '
        let newTotal = await query(newSql)
        let oldTotal = await query(oldSql)
        let secondaryTotal = await query(secondarySql)
        ctx.body = {
            code: 1,
            data: {
                newTotal: newTotal[0].total,
                oldTotal: oldTotal[0].total,
                secondaryTotal: secondaryTotal[0].total
            }
        }
    }
    //省市区
    sheng = async (ctx) => {
        let sql = `select * from provinces where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }
    shi = async (ctx) => {
        let { provinceid } = ctx.request.body
        let sql = `select * from cities where 1=1 `
        if (provinceid) {
            sql += `and provinceid=${provinceid}`
        }
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }

    }
    xian = async (ctx) => {
        let { cityid } = ctx.request.body
        let sql = `select * from areas where 1=1 `
        if (cityid) {
            sql += `and cityid=${cityid} `
        }
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }
    //筛选数据
    getPriceList = async (ctx) => {
        let priceSql = `select * from priceList where 1=1 ` //租金
        let directionSql = `select * from direction where 1=1 ` //方向
        let buyType = `select * from roomState where 1=1 `
        let roomeList = `select * from roomTypeList where 1=1 `
        let rentTypeList = `select * from rentType where 1=1 `
        let priceList = await query(priceSql)
        let direction = await query(directionSql)
        let buyList = await query(buyType)
        let roomTypeList = await query(roomeList)
        let rentType = await query(rentTypeList)
        ctx.body = {
            code: 1,
            body: {
                priceList,
                direction,
                buyList,
                roomTypeList,
                rentType
            }
        }
    }


    //新房
    getNewHom = async (ctx) => {
        let sql = `select * from newHouse where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }
    //根据地区查找
    areaFind = async (ctx) => {
        let { provinces, cities, areas, priceValue, rentType, roomType, direction } = ctx.request.body;
        let sql = `select * from newHouse where 1=1 `
        if (provinces && cities && areas) {
            sql += `and provinces like "%${provinces}%" and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        } else if (cities && areas) {
            sql += `and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        } else if (priceValue) {
            //房价筛选
            switch (priceValue) {
                case "LessThanOneHundredThousand": {  //≤10万
                    let sql = `select * from newHouse where price<${100000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                };
                case "TwoHundredThousandTOThree": { //20万～30万
                    let sql = `select * from newHouse where price>${200000} and price<${300000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                };
                case "ThreeHundredThousandTOfive": { //30万～50万
                    let sql = `select * from newHouse where price>${300000} and price<${500000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "FiveHundredThousandTOOne": { //50万～100万
                    let sql = `select * from newHouse where price>${500000} and price<${1000000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "MoreThanTenMillion": { //≥1000万
                    let sql = `select * from newHouse where price>${10000000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from newHouse where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (rentType) {
            switch (rentType) {
                case "new": { //新房
                    let sql = `select * from newHouse where state like "%${'新房'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "twoHead": { //二手房
                    let sql = `select * from newHouse where state like "%${'二手房'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from newHouse where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (roomType) {
            switch (roomType) {
                case "one": { //一室
                    let sql = `select * from newHouse where roomeType like "%${'一室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "two": { //二室
                    let sql = `select * from newHouse where roomeType like "%${'两室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "three": { //三室
                    let sql = `select * from newHouse where roomeType like "%${'三室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "threeAdd": { //三室+
                    let sql = `select * from newHouse where roomeType like "%${'三室+'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from newHouse where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (direction) { //方向
            switch (direction) {
                case 'east': { //东
                    let sql = `select * from newHouse where direction like "%${'东'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'east': { //东
                    let sql = `select * from newHouse where direction like "%${'东'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'west': { //西
                    let sql = `select * from newHouse where direction like "%${'西'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'south': { //南
                    let sql = `select * from newHouse where direction like "%${'南'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'north': { //北
                    let sql = `select * from newHouse where direction like "%${'北'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from newHouse where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        }
    }



    //二手房
    oldHouse = async (ctx) => {
        let sql = `select * from twoHoust where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }

    findTwo = async (ctx) => {
        let { provinces, cities, areas, priceValue, rentType, roomType, direction } = ctx.request.body;
        let sql = `select * from twoHoust where 1=1 `
        if (provinces && cities && areas) {
            sql += `and provinces like "%${provinces}%" and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        } else if (cities && areas) {
            sql += `and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        } else if (priceValue) {
            //房价筛选
            switch (priceValue) {
                case "LessThanOneHundredThousand": {  //≤10万
                    let sql = `select * from twoHoust where price<${100000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                };
                case "TwoHundredThousandTOThree": { //20万～30万
                    let sql = `select * from twoHoust where price>${200000} and price<${300000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                };
                case "ThreeHundredThousandTOfive": { //30万～50万
                    let sql = `select * from twoHoust where price>${300000} and price<${500000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "FiveHundredThousandTOOne": { //50万～100万
                    let sql = `select * from twoHoust where price>${500000} and price<${1000000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "MoreThanTenMillion": { //≥1000万
                    let sql = `select * from twoHoust where price>${10000000} `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from twoHoust where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (rentType) {
            switch (rentType) {
                case "new": { //新房
                    let sql = `select * from twoHoust where state like "%${'新房'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "twoHead": { //二手房
                    let sql = `select * from twoHoust where state like "%${'二手房'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from twoHoust where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (roomType) {
            switch (roomType) {
                case "one": { //一室
                    let sql = `select * from twoHoust where roomeType like "%${'一室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "two": { //二室
                    let sql = `select * from twoHoust where roomeType like "%${'两室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "three": { //三室
                    let sql = `select * from twoHoust where roomeType like "%${'三室'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "threeAdd": { //三室+
                    let sql = `select * from twoHoust where roomeType like "%${'三室+'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from twoHoust where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        } else if (direction) { //方向
            switch (direction) {
                case 'east': { //东
                    let sql = `select * from twoHoust where direction like "%${'东'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'east': { //东
                    let sql = `select * from twoHoust where direction like "%${'东'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'west': { //西
                    let sql = `select * from twoHoust where direction like "%${'西'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'south': { //南
                    let sql = `select * from twoHoust where direction like "%${'南'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case 'north': { //北
                    let sql = `select * from twoHoust where direction like "%${'北'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "everything": { //不限
                    let sql = `select * from twoHoust where 1=1  `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        }
    }






    //出租房
    secondary = async (ctx) => {
        let sql = `select * from House where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }

    rentHouse = async (ctx) => {
        let { provinces, cities, areas } = ctx.request.body;
        let sql = `select * from House where 1=1 `
        if (provinces && cities && areas) {
            sql += `and provinces like "%${provinces}%" and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        } else if (cities && areas) {
            sql += `and cities like "%${cities}%" and areas like "%${areas}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '筛选成功')
            } else {
                ctx.body = error('没有相关数据')
            }
        }
    }
}

module.exports = new Home()